<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="方案编码" prop="FNumber">
        <el-input v-model="queryParams.FNumber" placeholder="请输入方案编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mes:qc:plan:add']"
        >新增</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mes:qc:plan:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['mes:qc:plan:delete']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <!-- <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:qcscheme:export']"
        >导出</el-button> -->
        <el-button type="primary" plain icon="el-icon-plus" @click="synchronous" size="mini" v-hasPermi="['mes:qc:plan:sync']">同步数据</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="qcschemeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" width="50px">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="质检编码" align="center" prop="fnumber">
        <!-- <template slot-scope="scope">
          <el-link
            type="primary"
            @click="handleClick(scope.row)"
          >
            {{ scope.row.fnumber }}
          </el-link>
        </template>  -->
      </el-table-column>
      <el-table-column label="质检名称" align="center" prop="fname" />
      <el-table-column label="数据状态" align="center" prop="fdocumentStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.jdy_fdocumentstatus" :value="scope.row.fdocumentStatus" />
        </template>
      </el-table-column>
      <el-table-column label="生效日期" align="center" prop="feffectDate" />
      <el-table-column label="失效日期" align="center" prop="fexpireDate" />
      <el-table-column label="审核人" align="center" prop="fapproverId" />
      <el-table-column label="审核日期" align="center" prop="fapproveDate" />
      <el-table-column label="禁用状态" align="center" prop="fforbidStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.jdy_fforbidstatus" :value="scope.row.fforbidStatus" />
        </template>
      </el-table-column>
      <el-table-column label="禁用人" align="center" prop="fforbidderId" />
      <el-table-column label="禁用日期" align="center" prop="fforbidDate" />

      <el-table-column label="图片" align="center" prop="fimage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.fimage" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="适用范围" align="center" prop="fapplyScope" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['mes:qc:plan:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['mes:qc:plan:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改质检方案头对话框 -->
    <el-dialog :title="title" :visible.sync="open" width=70% append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" :inline="true">
        <el-form-item label="编码" prop="fnumber">
          <el-input v-model="form.fnumber" placeholder="请输入编码" />
        </el-form-item>
        <el-form-item label="生效日期" prop="FEffectDate">
          <el-date-picker clearable v-model="form.FEffectDate" type="date" value-format="yyyy-MM-dd" placeholder="请输入生效日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="失效日期" prop="FExpireDate">
          <el-date-picker clearable v-model="form.FExpireDate" type="date" value-format="yyyy-MM-dd" placeholder="请输入失效日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="适用范围" prop="FApplyScope">
          <el-input v-model="form.FApplyScope" placeholder="请输入适用范围" />
        </el-form-item>
        <el-form-item label="禁用人" prop="FForbidderId">
          <el-input v-model="form.FForbidderId" placeholder="请输入禁用人" />
        </el-form-item>
        <el-form-item label="禁用日期" prop="FForbidDate">
          <el-date-picker clearable v-model="form.FForbidDate" type="date" value-format="yyyy-MM-dd" placeholder="请输入禁用日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核人" prop="FApproverId">
          <el-input v-model="form.FApproverId" placeholder="请输入审核人" />
        </el-form-item>
        <el-form-item label="审核日期" prop="FApproveDate">
          <el-date-picker clearable v-model="form.FApproveDate" type="date" value-format="yyyy-MM-dd" placeholder="请输入审核日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="图片" prop="fimage">
          <image-upload v-model="form.fimage" />
        </el-form-item>
      </el-form>
      <el-divider content-position="center">质检方案</el-divider>
      <el-card shadow="always" class="box-card">
        <Productrecptline :id="form.id"></Productrecptline>
      </el-card>

      <el-divider content-position="center">适用范围</el-divider>
      <el-card shadow="always" class="box-card">
        <Productrecptscope v-if="open" :id="form.id" ref="Productrecptscope"></Productrecptscope>
      </el-card>

      <!-- <el-divider content-position="center">适用物料</el-divider> -->
      <el-card shadow="always" class="box-card">
        <MaterialsApplicable v-if="open" :id="form.id" ref="materialsApplicable"></MaterialsApplicable>
      </el-card>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listQcscheme, getQcscheme, delQcscheme, addQcscheme, updateQcscheme, synchronousHead, synchronousLine, synchronousRange, qcQmQcschemeentrySync } from "@/api/mes/qc/qcscheme";
import Productrecptline from "./line.vue"
import Productrecptscope from "./scopeApplication.vue"
import MaterialsApplicable from "./materialsApplicable.vue"
export default {
  name: "Qcscheme",
  components: { Productrecptline, Productrecptscope, MaterialsApplicable },
  dicts: [
    "jdy_fdocumentstatus",
    "jdy_fforbidstatus",
  ],
  data() {
    return {
      optType: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 质检方案头表格数据
      qcschemeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        workorderId: null,
        pageNum: 1,
        pageSize: 10,
        FID: null,
        FDocumentStatus: null,
        FForbidStatus: null,
        FNumber: null,
        FCreatorId: null,
        FModifierId: null,
        FCreateDate: null,
        FModifyDate: null,
        FForbidderId: null,
        FApproverId: null,
        FForbidDate: null,
        FApproveDate: null,
        FExpireDate: null,
        FEffectDate: null,
        FImage: null,
        FApplyScope: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fid: [
          { required: true, message: "金蝶云质检方案头表fid不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 同步 */
    synchronous() {
      qcQmQcschemeentrySync().then(msg => {
        if (msg.code == 200) {
          this.$modal.msgSuccess("操作成功！");
          this.getList();
        }
      })
      // this.queryParams.isAll = 0
      // var flog = true;
      // synchronousHead(this.queryParams).then(response => {
      //   if (response.code !== 200) {
      //     flog = false;
      //   }
      // });
      // synchronousLine(this.queryParams).then(response => {
      //   if (response.code !== 200) {
      //     flog = false;
      //   }
      // });
      // synchronousRange(this.queryParams).then(response => {
      //   if (response.code !== 200) {
      //     flog = false;
      //   }
      // });
      // if (flog) {
      //   this.$message({
      //     message: "数据同步成功",
      //     type: "success"
      //   });
      // } else {
      //   throw new Error("数据同步失败");
      // }
    },
    /** 查询质检方案头列表 */
    getList() {
      this.loading = true;
      listQcscheme(this.queryParams).then(response => {
        this.qcschemeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        FID: null,
        FDocumentStatus: null,
        FForbidStatus: null,
        FNumber: null,
        FCreatorId: null,
        FModifierId: null,
        FCreateDate: null,
        FModifyDate: null,
        FForbidderId: null,
        FApproverId: null,
        FForbidDate: null,
        FApproveDate: null,
        FExpireDate: null,
        FEffectDate: null,
        FImage: null,
        FApplyScope: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加质检方案头";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row, 'row');
      this.reset();
      const id = row.id || this.ids
      this.form = row
      this.open = true;
      this.title = "修改质检方案";
      // getQcscheme(id).then(response => {
      //   console.log(response,'修改的res');
      //   // this.form = response.data[0];
      //   this.open = true;
      //   this.title = "修改质检方案头";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateQcscheme(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addQcscheme(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const fnumber = row.fnumber
      this.$modal.confirm('是否确认删除质检方案头编码为"' + fnumber + '"的数据项？').then(function () {
        return delQcscheme(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mes/qc/qcscheme/export', {
        ...this.queryParams
      }, `qcscheme_${new Date().getTime()}.xlsx`)
    },
    handleClick(row) {
      const templateId = row.id || this.ids;
      getQcscheme(templateId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看检测模板信息";
        this.optType = "view";
      });
    }
  }
};
</script>
